package com.zhl.test;

import com.alibaba.druid.DbType;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws Exception {

        Properties pro = new Properties();
        InputStream is = Main.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);

        //获取连接池对象
//        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//        //5.获取连接
//        Connection conn = ds.getConnection();
//        System.out.println(conn);
//        System.out.println("Hello world!");
//
        String sql = "select *,s.name from student s where s.id=?";

        //支持的数据库类型在DbType中的枚举值
//        SQLStatement stmt = SQLUtils.parseSingleStatement(sql, DbType.mysql);
//        SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement();

        SQLSelectStatement stmt = (SQLSelectStatement) SQLUtils.parseSingleStatement(sql, DbType.mysql);
//        stmt.getSelect().addWhere(new SqlE)
        String sql_oracle = SQLUtils.toSQLString(stmt, DbType.oracle);
        System.out.println(sql_oracle);

//        PreparedStatement statement = conn.prepareStatement(sql);
//        statement.setInt(1, 1);
//        ResultSet resultSet = statement.executeQuery();
//
//        while (resultSet.next()) {
//            System.out.println("-----------------");
//            System.out.println("ID:" + resultSet.getInt("id") + " Name:" + resultSet.getString("name") + " Age:" + resultSet.getInt("age"));
//        }

        //6 释放资源
//        statement.close();




        //Druid Parser原理分析
        //SQLUtils.parseSingleStatement(sqlStr, DbType.mysql) 会调用SQLParserUtils.createSQLStatementParser(sql, dbType, features)
        // 获取到一个 SQLStatementParser的子类,如果是mysql,那么返回的类就是MySqlStatementParser。
    }
}